#include "u8g2.h"
#include "MYIIC.h"
#include "FreeRTOS.h"
#include "task.h"
#include "gd32f30x.h"
#include "delay.h"
u8g2_t u8g2;

/**
 * @brief   
 * 
 * @param   u8x8 
 * @param   msg 
 * @param   arg_int 
 * @param   arg_ptr 
 * @return  uint8_t 
 */
uint8_t u8x8_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
    uint8_t *data = (uint8_t *)arg_ptr;
    static uint8_t buffer[32];
    static uint8_t buf_idx;
    switch (msg)
    {
    case U8X8_MSG_BYTE_SEND:
        while (arg_int > 0)
        {
            buffer[buf_idx++] = *data++;
            arg_int--;
        }
        break;

    case U8X8_MSG_BYTE_INIT:
        MYIIC_Init(0x78);
        break;
    case U8X8_MSG_BYTE_SET_DC:
        break;
    case U8X8_MSG_BYTE_START_TRANSFER:
        buf_idx = 0;
        break;
    case U8X8_MSG_BYTE_END_TRANSFER:
        // if (HAL_I2C_Master_Transmit(&hi2c1, 0x78, buffer, buf_idx, HAL_MAX_DELAY) != HAL_OK)
        // {
        //     return 0;
        // }
    //    if(i2c_master_transmit(I2C0, 0x78, buffer, buf_idx) != I2C_STATUS_OK) 
    //    {
    //     return 0;
    //    }
    i2c_master_transmit(I2C0, 0x78, buffer, buf_idx);

        break;
    default:
        return 0;
    }
    return 1;
}

uint8_t u8g2_gpio_and_delay_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
{
    switch (msg)
    {
    case U8X8_MSG_GPIO_AND_DELAY_INIT:
        /* HAL initialization contains all what we need so we can skip this part. */
        break;
    case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
        delay_ms(arg_int);
        break;
    case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
        __NOP();
        break;
    case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
        delay_us(arg_int * 10);
        break;
    case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
        delay_us(1);
        __NOP();
        __NOP();
        break;              // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
    case U8X8_MSG_GPIO_I2C_CLOCK: // arg_int=0: Output low at I2C clock pin
        break;                    // arg_int=1: Input dir with pullup high for I2C clock pin
    case U8X8_MSG_GPIO_I2C_DATA:  // arg_int=0: Output low at I2C data pin
        break;                    // arg_int=1: Input dir with pullup high for I2C data pin
    case U8X8_MSG_GPIO_MENU_SELECT:
        u8x8_SetGPIOResult(u8x8, /* get menu select pin state */ 0);
        break;
    case U8X8_MSG_GPIO_MENU_NEXT:
        u8x8_SetGPIOResult(u8x8, /* get menu next pin state */ 0);
        break;
    case U8X8_MSG_GPIO_MENU_PREV:
        u8x8_SetGPIOResult(u8x8, /* get menu prev pin state */ 0);
        break;
    case U8X8_MSG_GPIO_MENU_HOME:
        u8x8_SetGPIOResult(u8x8, /* get menu home pin state */ 0);
        break;
    default:
        u8x8_SetGPIOResult(u8x8, 1); // default return value
        break;
    }
    return 1;
}

void u8g2_Init(u8g2_t *u8g2)
{
    u8g2_Setup_ssd1306_i2c_128x64_noname_f(u8g2, U8G2_R0, u8x8_byte_hw_i2c, u8g2_gpio_and_delay_stm32);
    // 初始化显示
    u8g2_InitDisplay(u8g2);
    // 关闭省电模式
    u8g2_SetPowerSave(u8g2, 0);
    // 清除缓冲区
    u8g2_ClearBuffer(u8g2);
}

void show_pic(u8g2_t *u8g2, u8g2_uint_t x, u8g2_uint_t y, u8g2_uint_t w, u8g2_uint_t h, const uint8_t *bitmap)
{
    u8g2_DrawXBMP(u8g2, x,  y,  w,  h, bitmap);
    u8g2_SendBuffer(u8g2);
}

// void show_wifi_connecting(void)
// {
//     u8g2_t u8g2;
//     u8g2_Init(&u8g2);
//     // show_pic(&u8g2, 0, 0, 15, 12, wifipic1_4);
//     // delay_ms(500);
//     // show_pic(&u8g2, 0, 0, 15, 12, wifipic1_2);
//     // delay_ms(500);
//     // show_pic(&u8g2, 0, 0, 15, 12, wifipic3_4);
//     // delay_ms(500);
//     // show_pic(&u8g2, 0, 0, 15, 12, wifipic_all);
//     // delay_ms(500);




//     show_pic(&u8g2, 40, 18, 36, 37, douyin);
// }
